package _29define.udp;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.util.Properties;
import java.util.Set;

public class Robot {
    public static void main(String[] args) throws IOException {
        DatagramSocket datagramSocket=new DatagramSocket(6565);

        new Thread(new ReserveRobot(datagramSocket)).start();
    }
}

class ReserveRobot implements Runnable{
    DatagramSocket datagramSocket;

    public ReserveRobot(DatagramSocket datagramSocket) {
        this.datagramSocket = datagramSocket;
    }

    @Override
    public void run() {
        Properties properties = new Properties();
        try {
            properties.load(new InputStreamReader(new FileInputStream("src/.editorconfig"),"GBK"));
            int dataCount = properties.size ();
            InetAddress targetIP=InetAddress.getByName("localhost");
            while (true){
                    byte[] bytes=new byte[1024];
                    DatagramPacket receiverPacket=new DatagramPacket(bytes,0,bytes.length);
                    datagramSocket.receive(receiverPacket);
                    int length=receiverPacket.getLength();
                    byte[] data=receiverPacket.getData();
                    int offset= receiverPacket.getOffset();
                    String msg=new String(data,offset,length);
                    if(msg.equals("end")){
                        break;
                    }
                    int count=0;
                    String responseMsg=null;
                    responseMsg=properties.getProperty(responseMsg);
                    if (responseMsg==null){
                        responseMsg="我不知道你在说什么，老汤我正在学习中";
                    }
                byte[] bytes4=responseMsg.getBytes();
                DatagramPacket sendPacket3=new DatagramPacket(bytes4,0,bytes4.length,targetIP,5656);
                datagramSocket.send(sendPacket3);
                    Set<String> stringKeys=properties.stringPropertyNames();
                    for (String s : stringKeys) {
                        if (s.equals(msg)){
                           String className=properties.getProperty(s);
                           byte[] bytes2=className.getBytes();
                           DatagramPacket sendPacket1=new DatagramPacket(bytes2,0,bytes2.length,targetIP,5656);
                           datagramSocket.send(sendPacket1);
                           break;
                        }
                        count++;
                    }
                    if (count==dataCount){
                        String a="我不知道你在说什么，老汤我正在学习中";
                        byte[] bytes3=a.getBytes();
                        DatagramPacket sendPacket2=new DatagramPacket(bytes3,0,bytes3.length,targetIP,5656);
                        datagramSocket.send(sendPacket2);
                    }
            }
            datagramSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}